package cn.ivicar.sms.service.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.carsmart.util.configmanagement.ConfigUtil;

public class SmsProperties {

    private static transient final Logger LOGGER = LoggerFactory.getLogger(SmsProperties.class);

    private static Properties properties = new Properties();

    static {
        try {
            InputStream inputStream = ConfigUtil.getConfigReader().getResourceAsStream("/sms.properties");
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(String.format("读取短信配置文件失败[sms.properties]。 ErrorMsg:[%s]", e.getMessage()));
        }
    }

    public static String getString(String propertiesKey) {
        return getString(propertiesKey, null);
    }

    public static int getInt(String propertiesKey) {
        return getInt(propertiesKey, 0);
    }

    public static boolean getBoolean(String propertiesKey, boolean defaultValue) {
        String v = properties.getProperty(propertiesKey);
        if (StringUtils.isBlank(v)) {
            return defaultValue;
        }
        return Boolean.parseBoolean(v);
    }

    public static String getString(String propertiesKey, String defaultValue) {
        return StringUtils.isBlank(properties.getProperty(propertiesKey)) ? defaultValue : properties
                .getProperty(propertiesKey);
    }

    public static int getInt(String propertiesKey, int defaultValue) {
        String str = properties.getProperty(propertiesKey);
        int v = defaultValue;
        if (StringUtils.isBlank(str)) {
            v = defaultValue;
        } else {
            try {
                v = Integer.parseInt(str);
            } catch (NumberFormatException e) {
                v = defaultValue;
            }
        }

        return v;
    }

    public static short getShort(String propertiesKey, short defaultValue) {
        String str = properties.getProperty(propertiesKey);
        short v = defaultValue;
        if (StringUtils.isBlank(str)) {
            v = defaultValue;
        } else {
            try {
                v = Short.parseShort(str);
            } catch (NumberFormatException e) {
                v = defaultValue;
            }
        }
        return v;
    }

    public static Properties getProperties() {
        return properties;
    }
}
